package com.msh.frame.common.util;


import com.msh.frame.client.define.StringDef;

import java.util.Random;
import java.util.UUID;

/**
 * @author shihu
 */
public class StringUtil {

  public static String uuid32() {
    return UUID.randomUUID().toString().replaceAll("-", "");
  }
  private static final String NUMBER_CHAR = "0123456789";
  private static final String LOWER_CASE_CHAR = "abcdefghijklmnopqrstuvwxyz";
  private static final String UPPER_CASE_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  public static final String TYPE_N = "n";
  public static final String TYPE_N_LC = "n_lc";
  public static final String TYPE_N_LC_UC = "n_lc_uc";
  private static final Random random = new Random();


  public static String randomStr(String type, Integer len){
    StringBuilder sb = new StringBuilder();
    if(TYPE_N.equals(type)){
      for (int i = 0; i < len; i++) {
        sb.append(NUMBER_CHAR.charAt(random.nextInt(10)));
      }
    }else if(TYPE_N_LC.equals(type)){
      for (int i = 0; i < len; i++) {
        int num = random.nextInt(36);
        if(num < 10){
          sb.append(NUMBER_CHAR.charAt(num));
        }else {
          sb.append(LOWER_CASE_CHAR.charAt(num-10));
        }
      }
    }else if(TYPE_N_LC_UC.equals(type)){
      for (int i = 0; i < len; i++) {
        int num = random.nextInt(62);
        if(num < 10){
          sb.append(NUMBER_CHAR.charAt(num));
        }else if(num < 36){
          sb.append(LOWER_CASE_CHAR.charAt(num-10));
        }else{
          sb.append(UPPER_CASE_CHAR.charAt(num-36));
        }
      }
    }
    return sb.toString();
  }

  public static String overLengthCut(String str, int maxLen) {
    if (str.length() > maxLen) {
      return str.substring(0, maxLen);
    }
    return str;
  }

  /**
   * 判断是否是空字符串 null和"" 都返回 true
   */
  public static boolean isEmpty(String s) {
    if (s != null && !s.equals(StringDef.EMPTY)) {
      return false;
    }
    return true;
  }

  public static boolean isNotEmpty(String s) {
    return !isEmpty(s);
  }

  /**
   * 首字母转小写
   */
  public static String toLowerCaseFirstOne(String s) {
    if (isEmpty(s)) {
      return s;
    }
    if (Character.isLowerCase(s.charAt(0))) {
      return s;
    } else {
      return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1))
          .toString();
    }
  }


  /**
   * 首字母转大写
   */
  public static String toUpperCaseFirstOne(String s) {
    if (isEmpty(s)) {
      return s;
    }
    if (Character.isUpperCase(s.charAt(0))) {
      return s;
    } else {
      return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1))
          .toString();
    }
  }


  /**
   * 下划线转头字母大写
   *
   * @param str hello_the_world
   * @return HelloTheWorld
   */
  public static String underlineToUpperCase(String str) {
    String[] strs = str.toLowerCase().split("_");
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < strs.length; i++) {
      builder.append(toUpperCaseFirstOne(strs[i]));
    }
    return builder.toString();
  }

  /**
   * 脱敏（小于等于长度时, 全部脱敏）
   * @param str 字符串
   * @param frontLen 前字符串长度
   * @param afterLen 后字符串长度
   * @return
   */
  public static String desensitize(String str, int frontLen, int afterLen){
    return desensitize(str, frontLen, afterLen, true);
  }

  /**
   * 脱敏
   * @param str 字符串
   * @param frontLen 前字符串长度
   * @param afterLen 后字符串长度
   * @param lteLastLenAllDesensitize 小于等于长度时, 是否全部脱敏
   * @return
   */
  public static String desensitize(String str, int frontLen, int afterLen, boolean lteLastLenAllDesensitize){
    if(null == str){
      return null;
    }
    int len = str.length();
    int lastLen = frontLen + afterLen;
    if(len <= lastLen){
      if(!lteLastLenAllDesensitize){
        return str;
      }
      StringBuilder sb = new StringBuilder();
      for(int i=0; i< len; i++){
        sb.append(StringDef.ASTERISK);
      }
      return sb.toString();
    }
    StringBuilder sb = new StringBuilder();
    sb.append(str.substring(0, frontLen));
    for(int i=0; i< len-lastLen; i++){
      sb.append(StringDef.ASTERISK);
    }
    sb.append(str.substring(len - afterLen));
    return sb.toString();
  }
}
